package com.have.blog.admin.controller;

import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.CommentDto;
import com.have.blog.admin.service.AdminCommentService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.core.annotion.avoidRepeatableCommit.AvoidRepeatableCommit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "用户评论相关接口", tags = {"用户评论相关接口"})
@RestController
@RequestMapping("/comment")
@Slf4j
public class AdminCommentController {
    @Autowired
    private AdminCommentService commentService;

    @ApiOperation(value = "获取评论列表", notes = "获取评论列表", response = String.class)
    @PostMapping(value = "/getPageList")
    public Wrapper<CommonPage<CommentDto>> getList(@Validated({GetList.class}) @RequestBody CommentDto commentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取评论列表: {}", commentDto);
        return commentService.getPageList(commentDto);
    }

    @AvoidRepeatableCommit
    @ApiOperation(value = "增加评论", notes = "增加评论", response = String.class)
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody CommentDto commentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("新增评论: {}", commentDto);
        return commentService.addComment(commentDto);
    }

    @ApiOperation(value = "编辑评论", notes = "编辑评论", response = String.class)
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody CommentDto commentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑评论: {}", commentDto);
        return commentService.editComment(commentDto);
    }

    @ApiOperation(value = "删除评论", notes = "删除评论", response = String.class)
    @PostMapping("/delete/{uid}")
    public Wrapper delete(@PathVariable(name = "uid", value = "uid") Integer uid) {

        // 参数校验
        log.info("删除评论: {}", uid);
        return commentService.deleteComment(uid);
    }

    @OperationLogger(value = "删除选中评论")
    @ApiOperation(value = "删除选中评论", notes = "删除选中评论", response = String.class)
    @PostMapping("/deleteBatch")
    public Wrapper deleteBatch(@RequestBody List<Integer> uids) {

        // 参数校验
        return commentService.deleteBatchComment(uids);
    }

}
